﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Zen4SyncDashboard.DAL
{
    public partial class publisher : IDataErrorInfo
    {
        #region - IDataErrorInfo Members -

        public string Error { get { return null; } }

        public string this[string columnName]
        {
            get { return GetValidationError(columnName); }
        }

        #endregion - IDataErrorInfo Members -

        #region - Validation -

        static readonly string[] ValidatedProperties = 
        {
            "publisher_name",
            "publisher_address",
            "publisher_database",
            "publisher_networkType",
            "publisher_securityType"
        };

        public bool IsValid()
        {
            foreach (string property in ValidatedProperties)
                if (GetValidationError(property) != null)
                    return false;

            return true;
        }

        string GetValidationError(string propertyName)
        {
            if (!ValidatedProperties.Contains(propertyName))
                return null;

            string error = null;

            switch (propertyName)
            {
                case "publisher_name":
                    error = ValidateName();
                    break;
                case "publisher_address":
                    error = ValidateAddress();
                    break;
                case "publisher_database":
                    error = ValidateDatabase();
                    break;
                case "publisher_networkType":
                    error = ValidateNetworkType();
                    break;
                case "publisher_securityType":
                    error = ValidateSecurityType();
                    break;
                default:
                    throw new ArgumentException("Unrecognized property: " + propertyName);
            }

            return error;
        }

        private string ValidateSecurityType()
        {
            if (securityType == null)
                return "A Security Type must be selected";

            return null;
        }
            
        private string ValidateNetworkType()
        {
            if (networkType == null)
                return "A Network Type must be selected";

            return null;
        }

        private string ValidateDatabase()
        {
            if (string.IsNullOrWhiteSpace(publisher_database))
                return "Database can't be empty";

            return null;
        }

        private string ValidateAddress()
        {
            if (string.IsNullOrWhiteSpace(publisher_address))
                return "Address can't be empty";

            return null;
        }

        private string ValidateName()
        {
            if (string.IsNullOrWhiteSpace(publisher_name))
                return "Name can't be empty";

            return null;
        }

        #endregion - Validation -
    }
}
